package com.oncliedisk.administrator.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.os.AsyncTask;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.mogujie.tt.R;
import com.oncliedisk.administrator.content.FileConfig;
import com.oncliedisk.administrator.model.FileModel;

import com.oncliedisk.administrator.utils.HttpUtils;

import java.io.IOException;
import java.util.List;

/**
 * Created by Administrator on 2015/12/3.
 */
public class FileAdapter extends BaseAdapter {
    private List<FileModel> data;
    private Context context;
    private LayoutInflater inflater;

    public FileAdapter(List<FileModel> data, Context context) {
        this.data = data;
        this.context = context;
        inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    @Override
    public int getCount() {
        return data.size();
    }

    @Override
    public Object getItem(int position) {
        return data.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.item_file, null);
            holder = new ViewHolder();
            holder.img = (ImageView) convertView.findViewById(R.id.file_img);
            holder.date = (TextView) convertView.findViewById(R.id.file_date);
            holder.name = (TextView) convertView.findViewById(R.id.file_name);
            holder.size = (TextView) convertView.findViewById(R.id.file_size);
            holder.check = (ImageView) convertView.findViewById(R.id.file_check);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        FileModel model = data.get(position);
        holder.name.setText(model.getName());
        int type = model.getType();
        int drawId = 0;
        switch (type) {
            case FileConfig.TYPE_DOC:
                if (model.getName().toUpperCase().contains(".DOC")) {
                    drawId = R.drawable.word;
                } else if (model.getName().toUpperCase().contains(".PPT")) {
                    drawId = R.drawable.ppt;
                } else if (model.getName().toUpperCase().contains(".XLSX")) {
                    drawId = R.drawable.biaoge;
                }
                //如果是word文档
                break;
            case FileConfig.TYPE_BT:
                //如果是bt种子
                drawId = R.drawable.bt;
                break;
            case FileConfig.TYPE_IMG:
                //如果是图片
                if(model.getBitmap() == null){
                    drawId = R.drawable.img;
                    new MyTask().execute(model.getPath(), position);
                }
                break;
            case FileConfig.TYPE_MUSIC:
                //如果是音乐
                drawId = R.drawable.music;
                break;
            case FileConfig.TYPE_OTHER:
                //如果是其他
                drawId = R.drawable.other;
                break;

            case FileConfig.TYPE_VIDEO:
                //如果是视频
                drawId = R.drawable.video;
                break;
            case FileConfig.TYPE_FOLDER:
                //如果是文件夹
                drawId = R.drawable.folder;
                break;
            default:
                drawId = R.drawable.bg;
                break;
        }
        if(model.getBitmap() == null){
            holder.img.setImageResource(drawId);
        }else{
            holder.img.setImageBitmap(model.getBitmap());
        }
        holder.size.setVisibility(View.GONE);
        holder.check.setVisibility(View.INVISIBLE);
        if (type != FileConfig.TYPE_FOLDER) {
            holder.size.setVisibility(View.VISIBLE);
            holder.size.setText(model.getSize());
        }
        holder.date.setText(model.getCreateTime());
        //如果是等待选中状态
        if (model.isLoading()) {
            holder.check.setVisibility(View.VISIBLE);
            holder.check.setImageResource(R.drawable.check);
        }
        //如果被选中
        if (model.isCheck()) {
            holder.check.setVisibility(View.VISIBLE);
            holder.check.setImageResource(R.drawable.check_blue);
        }
        //如果是正常状态
        if (model.isSingle() == true) {
            holder.check.setVisibility(View.INVISIBLE);
        }
        return convertView;

    }

    public class ViewHolder {
        ImageView img;
        TextView name;
        TextView size;
        TextView date;
        ImageView check;
    }

    public class MyTask extends AsyncTask<Object, Void, Bitmap> {

        private String path;
        private int position;

        @Override
        protected Bitmap doInBackground(Object... objects) {
            path = (String) objects[0];
            position = (int) objects[1];
            Bitmap netWorkBitmap = null;
            try {
                netWorkBitmap = HttpUtils.getBitmap(path);
            } catch (IOException e) {
                e.printStackTrace();
            }
            return netWorkBitmap;
        }

        @Override
        protected void onPostExecute(Bitmap bitmap) {
            try{
                super.onPostExecute(bitmap);
                data.get(position).setBitmap(bitmap);
                FileAdapter.this.notifyDataSetChanged();
            }catch (Exception ex){
                Log.i("info","图片加载出错");
            }

        }
    }


    public List<FileModel> getData() {
        return data;
    }
}
